﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace GempGame
{
    public class FactoryUpgrade
    {
        public double cost { get; set; }
        public int maxProd { get; set; }
        public double prodPrice { get; set; }
        public int number { get; set; }
        public double fixedCosts { get; set; }
    }

    public class Factory
    {
        public double productionPrice { get; set; }
        public double actualPrice { get; set; }
        public int actualProduction { get; set; }
        public double fixedCosts { get; set; }
        public int maxProd { get; set; }
        public double advisedPrice { get; set; }
        public int upgradeNumber { get; set; }
        public int stock { get; set; }
        public List<int> prodHistory { get; set; }
        public List<FactoryUpgrade> upgrades { get; set; }


        public Factory(string filename, string marketName)
        {
            this.upgrades = new List<FactoryUpgrade>();
            loadFactory(filename, marketName);
            this.maxProd = upgrades[0].maxProd;
            this.productionPrice = upgrades[0].prodPrice;
            this.fixedCosts = upgrades[0].fixedCosts;
            this.upgradeNumber = 0;
            this.stock = 0;
            this.actualPrice = this.advisedPrice;
            this.actualProduction = this.maxProd;
            prodHistory = new List<int>();
        }

        public FactoryUpgrade getNextUpgrade()
        {
            foreach (FactoryUpgrade upFac in this.upgrades)
            {
                if (upFac.number == this.upgradeNumber + 1)
                    return upFac;
            }
            FactoryUpgrade ret = new FactoryUpgrade();
            ret.number = -1;
            return ret;
        }

        public void upgrade()
        {

            FactoryUpgrade up = this.getNextUpgrade();
            if (up.number != -1)
            {
                this.maxProd = up.maxProd;
                this.productionPrice = up.prodPrice;
                this.upgradeNumber = up.number;
                this.fixedCosts = up.fixedCosts;
            }
        }

        public FactoryUpgrade getLowestUpgrade()
        {
            foreach (FactoryUpgrade upFc in this.upgrades)
            {
                if (upFc.number == 0)
                    return upFc;
            }
            return null;
        }


        public void loadFactory(string nomeFicheiro, string marketName) {
            XmlDocument loadFich = new XmlDocument();

            loadFich.Load(nomeFicheiro);
            XmlNodeList mark = loadFich.GetElementsByTagName("Market");

            foreach (XmlNode market in mark)
            {
                if (market["Name"].InnerXml.CompareTo(marketName) == 0)
                {
                    XmlNodeList factList = market.ChildNodes;

                    foreach (XmlNode factory in factList)
                    {
                        if (factory.Name.CompareTo("Factory") == 0)
                        {
                            //Numero de upgrades
                            int numUpgrades = int.Parse(factory.Attributes["numUpgrades"].InnerXml);
                            this.advisedPrice = double.Parse(factory.Attributes["advisedPrice"].InnerXml);

                            XmlNodeList upgradesList = loadFich.GetElementsByTagName("Upgrade");

                            //Percorre upgrades
                            foreach (XmlNode upgrade in upgradesList)
                            {
                                FactoryUpgrade upgradeFact = new FactoryUpgrade();

                                upgradeFact.number = int.Parse(upgrade.Attributes["num"].InnerXml);

                                foreach (XmlNode upgradeChild in upgrade.ChildNodes)
                                {
                                    if (upgradeChild.Name.CompareTo("MaxProduction") == 0)
                                    {
                                        upgradeFact.maxProd = int.Parse(upgradeChild.InnerXml);
                                    }
                                    else if (upgradeChild.Name.CompareTo("ProductionPrice") == 0)
                                    {
                                        upgradeFact.prodPrice = double.Parse(upgradeChild.InnerXml);
                                    }
                                    else if (upgradeChild.Name.CompareTo("UpgradeCost") == 0)
                                    {
                                        upgradeFact.cost = double.Parse(upgradeChild.InnerXml);
                                    }
                                    else if (upgradeChild.Name.CompareTo("FixedCosts") == 0)
                                    {
                                        upgradeFact.fixedCosts = double.Parse(upgradeChild.InnerXml);
                                    }
                                }

                                this.upgrades.Add(upgradeFact);
                            }
                        }
                    }
                }
            }
        }

    }
}
